package com.rt.easyjava.common.enumsUtils;

import com.rt.easyjava.common.base.Result;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

public class EnumsUtils {

    public static Result getData4AddBy(String enumName) throws Exception {
        enumName = enumName.substring(0, 1).toUpperCase() + enumName.substring(1);
        Object object = invokeStaticMethod("com.rt.easyjava.common.enums."+enumName,"toList",new Object[]{});
        List<Map<String,String>> list = (List<Map<String,String>>)object;
        return Result.builder(list).success();
    }

    public static Object invokeStaticMethod(String className, String methodName,
                                     Object[] args) throws Exception {
        Class ownerClass = Class.forName(className);
        Class[] argsClass = new Class[args.length];
        for (int i = 0, j = args.length; i < j; i++) {
            argsClass[i] = args[i].getClass();
        }
        Method method = ownerClass.getMethod(methodName,argsClass);
        return method.invoke(null, args);
    }



    // Vue elementui 控件后面有X按钮，这个方法目前没用到
    /*@ApiOperation("根据枚举名称返回数据字典相关列表(查询条件中使用)")
    @GetMapping(value = "/getData4ConditionBy")
    @ResponseBody
    public Result getData4ConditionBy(String enumName) throws Exception {
        Object object = invokeStaticMethod("com.rt.easyjava.common.enums."+enumName,"toList",new Object[]{});
        List<Map<String,String>> list = (List<Map<String,String>>)object;

        //查询条件中动态数据
        Map map = new HashMap();
        List<Map> copyList = new ArrayList<Map>();
        copyList.addAll( list );
        map.put("keyName","");
        map.put("valueName","全部");
        copyList.add(0, map);

        return Result.builder(copyList).success();
    }*/

}
